#!/bin/env ruby
# encoding: utf-8
require_relative '../spec/spec_helper'
require_relative '../spec/commons/user_auth'
require_relative '../spec/commons/check_pages'


describe 'Benefit page' do
	before  {
  }

 #    There are lot of inputs in a table (equal to benefits amount) + input for addition
 
 #### -  Inputs in table. 
 

 ###
  context "when logged in" do
    it "then he can work with system"  do
      goto 'http://pulse.7bits.it/en/login-page'
      sign_in("adm.company2@mail.ru", "123qwe+")
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      check_benefit_page()      
    end
  end
  ################################
  # For input new benefit
  # -- submit empty input is ignored
  # -- submit string with length more than 255 symbols:  “Title should contain no more than 255 symbols”
  # -- submit benefit with title, which is already exist: “Benefit with such title is already exist”

  # -- submit empty input is ignored
  context 'when add empty input' do
    it 'then title cannot be blank'  do     
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      text_field(name: "title", id: "new-entry-input-js", class: "form-control js-admin-add-input").clear     
      button(class: "btn btn-success js-admin-add-btn", text: "Add new").click        
      findEmpty = false
      tableA = table(class: "table table-hover js-admin-table-for-department table-striped").tbody      
      tableA.trs.each do |rows|   
        if (rows[0].element.value == "") then          
          findEmpty = true
          break              
        end   
      end
      findEmpty.should be_falsey
    end
  end

  # -- submit string with length more than 255 symbols:  “Title should contain no more than 255 symbols”  
  context 'when submit title' do
    it 'then it should contain no more than 255 symbols' do
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      ustr='a'
      257.times{|i| ustr+='a'}      
      text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").clear
      text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set ustr
      button(class: "btn btn-success js-admin-add-btn", text: "Add new").click             
      form(class: "form-inline js-admin-add-form has-error", method: "post")
          .span(class: "help-block admin-helper", text: "Title should contain no more than 255 symbols").wait_until_present(5)
     # browser.refresh      
    end
  end
  #    .div(class: "row")
      #    .div(class: "form-group col-sm-4 col-sx-6")
      #    .div(class: "input-group") 
  # -- submit benefit with title, which is already exist: “Benefit with such title is already exist”
  context 'when input title with existing name' do
    it 'then benefit with such title is already exist' do
      #get length of table of benefits
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      tableA = table(class: "table table-hover js-admin-table-for-department table-striped").tbody      
      lengthA = tableA.trs.length
      if  lengthA == 0  then 
        text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set "123"
        button(class: "btn btn-success js-admin-add-btn", text: "Add new").click
        text = "123"
      else 
        text = tableA[0][0].element.value
      end
      text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").clear
      text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set text
      button(class: "btn btn-success js-admin-add-btn", text: "Add new").click      
      span(class: "help-block", text: "Benefit with such title is already exist").wait_until_present(5)

    end
  end

  ################################
  # For inputs in table of benefit
  # -- submit empty input is ignored
  # -- submit string with length more than 255 symbols:  “Title should contain no more than 255 symbols”
  # -- submit benefit with title, which is already exist: “Benefit with such title is already exist”

  # -- submit empty input is ignored
  context "when submit empty input in table's cell" do
    it "then submit ignored" do
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      tableA = table(class: "table table-hover js-admin-table-for-department table-striped").tbody      
      lengthA = tableA.trs.length
      #rem old value
      if  lengthA == 0  then 
        text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set "123"
        button(class: "btn btn-success js-admin-add-btn", text: "Add new").click        
        oldValue = "123"
      else 
        oldValue = tableA[0][0].text_field.value
      end      
      tableA[0][0].text_field.set ""
      tableA[0][1].button.click
      #goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      browser.refresh
      (tableA[0][0].text_field.value == oldValue).should be_truthy                
      STDERR.puts "1 ****" + tableA[0][0].text_field.value      
    end
  end

   # -- submit string with length more than 255 symbols:  “Title should contain no more than 255 symbols”
  context 'when submit in cell string with length more than 255 symbols' do  
    it 'then input should contain no more 255 symbols' do
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      tableA = table(class: "table table-hover js-admin-table-for-department table-striped").tbody      
      lengthA = tableA.trs.length        

      if  lengthA == 0  then 
        text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set "123"
        button(class: "btn btn-success js-admin-add-btn", text: "Add new").click                
      end
      ustr='a'
      257.times{|i| ustr+='a'}      
      oldValue = tableA[0][0].text_field.value
      tableA[0][0].text_field.set ustr
      tableA[0][1].button.click
      tableA[0][0].span(class: "help-block dark-red", text: "Title should contain no more than 255 symbols").wait_until_present(3)        
      browser.refresh
      (tableA[0][0].text_field.value == oldValue).should be_truthy
    end    
  end

   # -- submit benefit with title, which is already exist: “Benefit with such title is already exist”
  context 'when submit title with existing name' do
    it 'then benefits with that name already exists' do
      goto "http://pulse.7bits.it/en/us/admin/benefits-list"
      tableA = table(class: "table table-hover js-admin-table-for-department table-striped").tbody      
      lengthA = tableA.trs.length

      if  lengthA == 0    then 
        text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set "123"
        button(class: "btn btn-success js-admin-add-btn", text: "Add new").click                
        text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set "asd"
        button(class: "btn btn-success js-admin-add-btn", text: "Add new").click
        someValue = "asd"      
      else 
        if lengthA == 1 then
          someValue = "@"+ tableA[0][0].text_field.value.to_s[1,3]
          text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title").set  someValue          
             STDERR.puts "someValue = " + "@"+ tableA[0][0].text_field.value.to_s[1,3]
          button(class: "btn btn-success js-admin-add-btn", text: "Add new").click    
          else #lengthA > 1                         
          someValue = tableA[1][0].text_field.value
        end
      end      
      browser.refresh 

      tableA[1][0].text_field.set tableA[0][0].text_field.value
      tableA[1][1].button.click
      tableA[1][0].span(class: "help-block dark-red", text: "Benefit with such title is already exist").wait_until_present(5)        
      browser.refresh

      if (lengthA == 0) then
        tableA[1][2].a(href: "#", class: "btn-sm btn btn-default remove-link js-admin-remove-button", text: 'Remove').click                
        tableA[0][2].a(href: "#", class: "btn-sm btn btn-default remove-link js-admin-remove-button", text: 'Remove').click                
      else
        if lengthA == 1 then
          tableA[1][2].a(href: "#", class: "btn-sm btn btn-default remove-link js-admin-remove-button", text: 'Remove').click
          else 
          tableA[1][0].text_field.value = someValue 
        end
      end
      browser.refresh
      (lengthA == tableA.trs.length).should be_truthy      
    end


  end

  context "when logged out" do
    it "then he can't work with system"  do
      user_logout()
    end
  end
 

end 